define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'refund/index' + location.search,
                    add_url: 'refund/add',
                    edit_url: 'refund/edit',
                    del_url: 'refund/del',
                    multi_url: 'refund/multi',
                    import_url: 'refund/import',
                    table: 'refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'refund_no', title: __('退款单号'), operate: 'LIKE'},
                        {field: 'user.username', title: __('用户名'), operate: 'LIKE'},
                        {field: 'order.order_sn', title: __('订单号'), operate: 'LIKE'},
                        {field: 'total_fee', title: __('订单金额'), operate:'BETWEEN'},
                        {field: 'refund_fee', title: __('退款金额'), operate:'BETWEEN'},
                        {field: 'status', title: __('状态'), searchList: {"0":'处理中',"1":'成功',"2":'失败'}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('申请时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('更新时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-detail',
                                    url: 'refund/detail'
                                },
                                {
                                    name: 'sync',
                                    text: __('同步状态'),
                                    icon: 'fa fa-refresh',
                                    classname: 'btn btn-xs btn-warning btn-sync',
                                    url: 'refund/sync',
                                    visible: function(row) {
                                        return row.status == 0;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            // 关闭按钮
            $(document).on('click', '.btn-close', function () {
                Fast.api.close();
            });
            
            // 同步按钮
            $(document).on('click', '.btn-sync', function () {
                var that = this;
                Layer.confirm('确定要同步退款状态吗？', {
                    title: '同步退款状态',
                    btn: ['确定', '取消']
                }, function (index) {
                    Fast.api.ajax({
                        url: $(that).data('url'),
                        data: {ids: $(that).data('ids')},
                    }, function (data, ret) {
                        Layer.close(index);
                        Layer.msg(ret.msg);
                        setTimeout(function () {
                            location.reload();
                        }, 1500);
                    });
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});